package com.blog.extend;

import com.blog.aop.WeiboRequest;
import com.blog.model.TestDto;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 微博请求测试
 *
 * @author Joy
 * @date 2020-02-19
 */
public class WeiboRequestResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterType().isAssignableFrom(TestDto.class) && methodParameter.hasParameterAnnotation(
            WeiboRequest.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,
        NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        String ip = nativeWebRequest.getParameter("ip");
        Integer campaignId = Integer.valueOf(nativeWebRequest.getParameter("campaignid"));
        Long clickTime = Long.valueOf(nativeWebRequest.getParameter("clicktime"));
        String imei = nativeWebRequest.getParameter("imei_md5");
        String imp = nativeWebRequest.getParameter("IMP");
        TestDto testDto = new TestDto();
        testDto.setIp(ip);
        testDto.setCampaignid(campaignId);
        testDto.setClicktime(clickTime);
        testDto.setImei(imei);
        testDto.setImp(imp);
        return testDto;
    }
}
